<template>
    <div id='' class='system_set_form'>
        <i-form ref="iForm" :config="config">
            <template #nrms="{row}">
                <div class="iquill">
                    <i-quill v-model="nrms" :min-height="300" :max-height="300" ref="quill" />
                </div>
            </template>
            <template #fj="{row}">
                <div v-if="file.length !== 0" class="imgShow">
                    <a>{{file[0].name}}.{{file[0].ext}}</a>
                    <Icon v-if="!infoUpload" type="ios-close-circle" class="icon_close" size="15" color="#ff9900" @click="removeImg" />
                </div>
                <Upload v-else :disabled="infoUpload==true" :show-upload-list="false" type="drag" action="" :accept="Accept" :format="Format" :before-upload="beforeUpload">
                    <div style="padding: 20px 0">
                        <Icon type="ios-cloud-upload" size="52" style="color: #3399ff"></Icon>
                        <p>点击或拖拽文件上传,文件大小不能超过50MB</p>
                    </div>
                </Upload>
            </template>
        </i-form>
    </div>
</template>
<script>
import { config } from './policyform_config'
import iQuill from '@/components/quill';
export default {
    name: '',
    props: [''],
    data() {
        this.newConfig = icu.util.deepClone(config)
        return {
            config: this.newConfig,
            formData: {},
            nrms: '',
            file: [],
            showImgPath: '',
            infoUpload: false,
            Accept: ".pdf,.docx,.doc,.xlsx,.png,.jpg,.jpeg,",
            Format: ['.pdf', '.docx', '.doc', '.xlsx', '.png', '.jpg', '.jpeg']
        };
    },
    components: { 'i-quill': iQuill },
    computed: {},
    beforeMount() { },
    mounted() {
        this.$fileApi = this.$Api.get(['common:upload'])
        let userInfo = icu.storage.getData('userInfo');
        if (userInfo.extend) {
            this.$refs.iForm.setData({
                releaseDept: userInfo.extend.tenantRgionLabel
            })
        }
    },
    methods: {
        getForm(cb) {
            this.$refs.iForm.submit('infoAddUpdata', (res) => {
                this.formData = this.returnData(res);
                this.$http.$post('InformationSave', { data: this.formData }).then(res => {
                    if (res.code == 200) {
                        this.$Message.success('保存成功');
                        if (cb) {
                            cb(this.formData)
                        }
                        return
                    } else {
                        if (res.msg == 'contentStr:发布内容不能为空') {
                            this.$Message.error('内容描述不能为空')
                        }
                    }
                })
            })
        },
        setData(row, value) {
            this.$http.$get('InformationDetails', { params: { id: row.id } }).then(res => {
                if (res.code == 200) {
                    if (res.data.doc) {
                        this.file.push({ name: `${res.data.doc.name}` + `.` + `${res.data.doc.ext}` })
                        // this.file.push({ id: res.data.docId })
                        // this.showImgPath = window.config.fileServer + 'show/' + `${res.data.doc.minioPath}`
                    }
                    this.nrms = res.data.contentStr
                    this.$refs.iForm.setData({
                        ...this.returnData(res.data)
                    })
                    return
                }
                this.$Message.error(res.msg);
            })
            if (value == 'info') {
                this.$nextTick(() => {
                    this.$refs.quill.Quill.enable(false)
                })
                this.infoUpload = true
                for (let i = 0; i < this.config.form.length; i++) {
                    this.config.form[i].disabled = true
                }
                return
            }
        },
        returnData(res) {
            return {
                type: res.type ? res.type : '',
                contentStr: this.nrms,
                docId: this.file.length == 0 ? '' : this.file[0].id,
                releaseDept: res.releaseDept ? res.releaseDept : '',
                title: res.title ? res.title : '',
                id: res.id ? res.id : '',
            }
        },
        beforeUpload(response) {
            this.$Message.loading({
                content: '正在上传',
                duration: 0
            });
            // response.size 单位是 b 字节
            if (response.size > 51200 * 1024) {
                this.$Message.warning('文件  ' + response.name + ' 太大，上传文件大小不能超过500MB.');
                return
            }
            // 调用后台接口，上传附件，返回附件信息，进行处理【建议缓存附件id，与表单进行关联
            this.$fileApi.fileUpload(response, (res) => {
                if (res.code == 200) {
                    this.showImgPath = window.config.fileServer + 'show/' + `${res.data[0].minioPath}`
                    this.file = res.data;
                    this.$Message.destroy()
                } else {
                    this.$Message.error(res.msg);
                }
            })
        },
        removeImg() {
            this.file = []
            this.showImgPath = ''
        }
    },
    watch: {}
}
</script>
<style lang='less' scoped>
.iquill {
    border: 1px dashed #dcdee2;
    border-radius: 4px;
}
.iquill:hover {
    border: 1px dashed rgb(51, 153, 255);
    border-radius: 4px;
}
.imgShow {
    margin-left: 10px;
    position: relative;
    width: auto;
    height: 30px;
}
.imgShow .icon_close {
    position: absolute;
    right: 0px;
    top: 10px;
}
</style>
